package dao.impl;

import dao.UserDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pojo.User;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public List<User> findAll() {
       //引入mybatis配置文件
        SqlSession sqlSession = null;

        String resource = "mybatis-config.xml";
        try {
            //加载配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);

            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            sqlSession = sqlSessionFactory.openSession();

        List<User> users = sqlSession.getMapper(UserDao.class).findAll();

        return users;
        }catch (IOException e){
        throw new RuntimeException(e);
    }finally {
        sqlSession.close();
    }


    }
}
